//
//  Utility.m
//  Alfresco
//
//  Created by Michael Muller on 10/14/09.
//  Copyright 2010 Zia Consulting.  Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 
//


#import "Utility.h"

NSString* createStringByEscapingAmpersandsInsideTagsOfString(NSString *input, NSString *startTag, NSString *endTag) {
	
	NSMutableString *escapedString = [[NSMutableString alloc] initWithString:@""];
	
	NSArray *pieces = [input componentsSeparatedByString:startTag];
	
	if ([pieces count] > 0) {
		[escapedString appendString:[pieces objectAtIndex:0]];
		
		for (int i = 1; i < [pieces count]; i++) {
			
			NSString *piece = [pieces objectAtIndex:i];
			NSRange r = [piece rangeOfString:endTag];
			
			NSString *firstHalf = [piece substringToIndex:r.location];
			NSString *secondHalf = [piece substringFromIndex:r.location];
			NSString *encodedFirstHalf = [firstHalf stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"]; 
			
			[escapedString appendString:startTag];
			[escapedString appendString:encodedFirstHalf];
			[escapedString appendString:secondHalf];
		}
	}
	
	return escapedString;
}

UIImage* imageForFilename(NSString* filename) 
{
	NSString *imageName = @"document.png";
	NSDictionary *mapping = [NSDictionary dictionaryWithObjectsAndKeys:
							 @"image.png", @".png", 
							 @"image.png", @".jpeg", 
							 @"image.png", @".jpg", 
							 @"image.png", @".gif", 
							 @"word.png", @".doc", 
							 @"word.png", @".docx", 
							 @"excel.png", @".xls", 
							 @"excel.png", @".xlsx", 
							 @"powerpoint.png", @".ppt", 
							 @"powerpoint.png", @".pptx", 
							 @"xml.png", @".xml", 
							 @"txt.png", @".txt", 
							 @"pdf.png", @".pdf", 
							 @"archive.png", @".zip", 
							 @"archive.png", @".sit", 
							 @"archive.png", @".gz", 
							 @"archive.png", @".tar", 
							 @"audio.png", @".mp3", 
							 @"audio.png", @".wav", 
							 @"image.png", @".tif", 
							 @"image.png", @".tiff", 
							 nil];
	NSUInteger location = [filename rangeOfString:@"." options: NSBackwardsSearch].location;
	if (location != NSNotFound) {
		NSString *ext = [[filename substringFromIndex:location] lowercaseString];
		if ([mapping objectForKey:ext]) {
			imageName = [mapping objectForKey:ext];
		}
	}
	
	return [UIImage imageNamed:imageName];
}

static int spinnerCount = 0;

void startSpinner() {
	if (spinnerCount <= 0) {
		spinnerCount = 0;
		[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
	}
	spinnerCount++;
}

void stopSpinner() {
	spinnerCount--;
	if (spinnerCount <= 0) {
		spinnerCount = 0;
		[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
	}
}



NSString* userPrefUsername() {
	return [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
}

NSString* userPrefPassword() {
	return [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
}

NSString* userPrefHostname() {
	return [[NSUserDefaults standardUserDefaults] objectForKey:@"host"];
}

NSString* userPrefPort() {
	return [[NSUserDefaults standardUserDefaults] objectForKey:@"port"];
}

NSString* userPrefWebApp() {
	return [[NSUserDefaults standardUserDefaults] objectForKey:@"webapp"];
}

BOOL userPrefShowHiddenFiles() {
	return [[NSUserDefaults standardUserDefaults] boolForKey:@"showHidden"];
}

BOOL userPrefEnableAmpersandHack() {
	return [[NSUserDefaults standardUserDefaults] boolForKey:@"ampersandHack"];
}

BOOL userPrefShowCompanyHome() {
	return [[NSUserDefaults standardUserDefaults] boolForKey:@"showCompanyHome"];
}

NSString* userPrefProtocol() {
	return [[NSUserDefaults standardUserDefaults] objectForKey:@"protocol"];
}
